Object.defineProperty(exports, "__esModule", {
  value: true
});
var def_http_request = function () {
  function _ctor() {
    this.objFunc = {};
  }
  _ctor.getInstance = function () {
    _ctor._instance || (_ctor._instance = new _ctor());
    return _ctor._instance;
  };
  _ctor.prototype.postRequest = function (t, e, o) {
    console.log("postRequest --->", t);

    const n = new XMLHttpRequest();
    n.addEventListener("error", this.onRequestError);
    n.open("POST", t, true);
    n.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    // 构建请求体
    const body = Object.keys(e)
      .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(e[key])}`)
      .join("&");

    n.send(body);

    // 处理响应
    n.onreadystatechange = function () {
      if (n.readyState === 4) {
        if (n.status === 200) {
          const response = JSON.parse(n.responseText);
          if (o) {
            o(response); // 调用回调函数，传入解析的结果
          }
        }
      }
    };
  };
  _ctor.prototype.get = function (t, e, o) {
    const n = cc.loader.getXMLHttpRequest();
    n.timeout = 10000; // 设置请求超时为10秒
    n.open("GET", t, true);
    n.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");

    n.onreadystatechange = function () {
      if (this.readyState === 4) {
        if (this.status >= 200 && this.status < 300) {
          const responseText = n.responseText;
          if (!responseText) {
            console.log("httperror", responseText);
            return;
          }
          const responseData = JSON.parse(responseText);
          o(responseData); // 调用回调函数，传入解析结果
        }
      }
    };

    // 构建请求体
    const params = Object.keys(e)
      .map(key => `${encodeURIComponent(key)}=${encodeURIComponent(e[key])}`)
      .join("&");

    // 发送请求
    n.send(params);
  };
  _ctor.prototype.setRequestErrorFunc = function (t, e) {
    this.objFunc.error = {
      cb: t,
      target: e
    };
  };
  _ctor.prototype.onRequestError = function () {
    if (this.objFunc.error) {
      var t = this.objFunc.error;
      t.cb.call(t.target, "游戏维护中");
    }
  };
  _ctor.url_ad = "https://ad.geyian.ink/";
  _ctor.url_login = "https://mljyd.youdongxi.cn/";
  _ctor.url_gamelevel = "https://img.youdongxi.cn/";
  _ctor.url_buy = "https://tj.geyian.ink/";
  _ctor._instance = null;
  return _ctor;
}();
exports.default = def_http_request;